CMAKE_MINIMUM_REQUIRED(VERSION 3.0)
PROJECT(mscripter CXX)
SET(TARGET_NAME ${PROJECT_NAME})

#==============================================================================
# SOURCE FILES MANAGE
#==============================================================================

AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src DATA_MODEL_SRC_LIST)
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src/misaka2d MISAKA2D_SRC_LIST)
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src/misakaEngine MISAKA_ENGINE_SRC_LIST)

SET(SRC_LIST 
	${DATA_MODEL_SRC_LIST}
	${MISAKA2D_SRC_LIST}
	${MISAKA_ENGINE_SRC_LIST}
	)


#==============================================================================
# DETECT SDL2 
#==============================================================================
SET(ARCH "i686")
IF(CMAKE_SIZEOF_VOID_P MATCHES "8")
    SET(ARCH "x86_64")
ENDIF(CMAKE_SIZEOF_VOID_P MATCHES "8")

IF(CMAKE_SYSTEM_NAME MATCHES "Windows")
    MESSAGE(STATUS "DETECTED WINDOWS OPERATING SYSTEM, USING MINGW32 BUILD SYSTEM.")
    
    SET(SDL2_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${ARCH}-w64-mingw32/include")
    SET(SDL2_LIBRARY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${ARCH}-w64-mingw32/lib")
    
    SET(SDL2_CXX_FLAGS "-mwindows -Dmain=SDL_main")
    SET(SDL2_LINK_LIBRARIES -lmingw32 -lSDL2main -lSDL2 )

ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux")
    MESSAGE(STATUS "DETECTED LINUX OPERATING SYSTEM")

    FIND_PATH(SDL2_INCLUDE_DIR 
        NAMES SDL.h 
        PATHS 
        "/usr/include/SDL2" 
        "/usr/local/include/SDL2"
            )

    IF(SDL2_INCLUDE_DIR MATCHES "NOTFOUND")
        MESSAGE(FATAL_ERROR "CAN NOT FIND SDL.h IN ")
    ENDIF(SDL2_INCLUDE_DIR MATCHES "NOTFOUND")

    IF(ARCH MATCHES "i686")
        
        FIND_PATH(SDL2_LIBRARY_DIR 
            NAMES libSDL2.a 
            PATHS 
                "/lib" 
                "/usr/lib" 
                "/usr/local/lib"
                )
    
    ELSE(ARCH MATCHES "i686")

        FIND_PATH(SDL2_LIBRARY_DIR 
            NAMES libSDL2.a 
            PATHS 
                "/lib64"
                "/usr/lib64"
                "/usr/local/lib64"
                )
    
    ENDIF(ARCH MATCHES "i686")

    IF(SDL2_LIBRARY_DIR MATCHES "NOTFOUND")
        MESSAGE(FATAL_ERROR "CAN NOT FIND libSDL2.a")
    ENDIF(SDL2_LIBRARY_DIR MATCHES "NOTFOUND")
    
    SET(SDL2_CXX_FLAGS "-Dmain=SDL_main")
    SET(SDL2_LINK_LIBRARIES -lSDL2main -lSDL2 )

ENDIF(CMAKE_SYSTEM_NAME MATCHES "Windows")

MESSAGE(STATUS "CONFIGURE SDL2 INCLUDE PATH ... ${SDL2_INCLUDE_DIR}")
MESSAGE(STATUS "CONFIGURE SDL2 LIBRARY PATH ... ${SDL2_LIBRARY_DIR}")
MESSAGE(STATUS "CONFIGURE SDL2 CXX FLAGS ... ${SDL2_CXX_FLAGS}")
MESSAGE(STATUS "CONFIGURE SDL2 LIBRARIES ... ${SDL2_LINK_LIBRARIES}")

#==============================================================================
# CONFIGURE COMPILER OPTIONS
#==============================================================================

INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIR})
LINK_DIRECTORIES(${SDL2_LIBRARY_DIR})

INCLUDE(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" IS_SUPPORT_CXX0X)
CHECK_CXX_COMPILER_FLAG("-std=c++11" IS_SUPPORT_CXX11)
IF(NOT IS_SUPPORT_CXX11 AND NOT IS_SUPPORT_CXX0X)
    MESSAGE(FATAL_ERROR "c++11 support is required.Please use another compiler.")
ENDIF(NOT IS_SUPPORT_CXX11 AND NOT IS_SUPPORT_CXX0X)

SET(CMAKE_CXX_FLAGS "${SDL2_CXX_FLAGS} -Wall -Wextra -pedantic -std=c++0x")
SET(CMAKE_CXX_FLAGS_RELEASE "-O2")

#==============================================================================
# CONFIGURE LINK OPTIONS
#==============================================================================

ADD_EXECUTABLE(${TARGET_NAME} ${SRC_LIST})
MESSAGE(STATUS "TARGET NAME: ${TARGET_NAME}")
TARGET_LINK_LIBRARIES(${TARGET_NAME} ${SDL2_LINK_LIBRARIES})

